/* 获取各种列表做下拉框，其他用户操作 */
import axios from 'axios'
import {MessageBox, Message} from 'element-ui'
// axios.defaults.baseURL = 'http://xqz.wecozhiku.com/index.php/Admin'


// axios.defaults.baseURL = 'http://api.xiaoqunzhi.com/Ces'
axios.defaults.baseURL = 'http://api.xiaoqunzhi.com/Admin'
/**
 * 获取商家列表
 * */
function getMerList (callback) {
  axios.get('/type/getmemberarr?type=2')
    .then(res => {
      if (res.data.status === '1' || res.data.status === 1) {
        callback(res.data.data)
      } else {
        callback(null)
      }
    })
}

/**
 * 普通用户
 * */
function getComList (callback) {
  axios.get('/type/getmemberarr?type=1')
    .then(res => {
      if (res.data.status === '1' || res.data.status === 1) {
        callback(res.data.data)
      } else {
        callback(null)
      }
    })
}

/**
 * 获取所以用户
 * */
function getAllUser (callback) {
  axios.get('/type/getmemberarr')
    .then(res => {
      if (res.data.status === '1' || res.data.status === 1) {
        callback(res.data.data)
      } else {
        callback(null)
      }
    })
}

// /Organization/index
/**
 * 获取所以用户zu
 * */
function getUserGroup (callback) {
  axios.get('/Organization/index')
    .then(res => {
      if (res.data.status === '1' || res.data.status === 1) {
        callback(res.data.data.list)
      } else {
        callback(null)
      }
    })
}

/**
 * 获取所有角色
 * */
function getRole (callback) {
  axios.get('/Rbac/role', {params: {page: 1, size: 100}})
    .then(res => {
      if (res.data.status === '1' || res.data.status === 1) {
        callback(res.data.data.list)
      } else {
        callback(null)
      }
    })
}

/**
 * 获取裙子列表
 * */
function getSkirtList (callback) {
  axios.get('/type/getskirtiarr')
    .then(res => {
      if (res.data.status === '1' || res.data.status === 1) {
        callback(res.data.data)
      } else {
        callback(null)
      }
    })
}

function getNewsList (callback) {
  axios.get('/type/gerplacardarra')
    .then(res => {
      if (res.data.status === '1' || res.data.status === 1) {
        callback(res.data.data)
      } else {
        callback(null)
      }
    })
}

function getTopicsList (callback) {
  axios.get('/type/getarticlearr')
    .then(res => {
      if (res.data.status === '1' || res.data.status === 1) {
        callback(res.data.data)
      } else {
        callback(null)
      }
    })
}

// /type/getrepoarr
function getRepoList (callback) {
  axios.get('/type/getrepoarr')
    .then(res => {
      if (res.data.status === '1' || res.data.status === 1) {
        callback(res.data.data)
      } else {
        callback(null)
      }
    })
}

/**
 * 删除功能
 * */
function deleteRow ({url, id}, callback) {
  MessageBox.confirm('此操作将删除该记录, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    axios.get(url, {params: {id}})
      .then(res => {
        callback(res)
      })
  })
}

/**
 * 删除原因
 * */
function deletePrompt ({url, id, memberId, title}, callback) {
  MessageBox.prompt('请输入删除原因', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  }).then(({ value }) => {
    let params = {id, memberid: memberId, delcontent: value}
    if (title) {
      params.title = title
    }
    axios.get(url, {params})
      .then(res => {
        callback(res)
      })
  })
}

/**
 * 导出功能
 * */
function toExport (url, params, title, callback) {
  axios.get(url, {params, responseType: 'blob'})
    .then(res => {
      callback()
      if (res.data.type === 'application/vnd.ms-excel') {
        let xls = window.URL.createObjectURL(res.data)
        let a = document.createElement('a')
        a.href = xls
        a.download = title + '.xls'
        a.click()
        window.URL.revokeObjectURL(xls)
      } else if (res.data.type === 'application/json') {
        let reader = new FileReader()
        reader.onload = () => {
          let content = JSON.parse(reader.result)
          Message.warning(content.msg)
        }
        reader.readAsText(res.data)
      }
    }).catch(() => {
      callback()
    })
}

/**
 * 获取话题
 * */
function getPosts (callback) {
  axios.get('/type/gettopicarr')
    .then(res => {
      if (res.data.status === '1' || res.data.status === 1) {
        callback(res.data.data)
      } else {
        callback(null)
      }
    }).catch(() => {
      callback(null)
    })
}

/**
 * 获取上个月
 * */
function getLastMouth () {
  let nowDate = new Date()
  let year = nowDate.getFullYear()
  let month = nowDate.getMonth()
  if (month === 0) {
    month = 12
    year = year - 1
  }
  let lastDay = new Date(year, month, 0)
  let start = `${year}-${month}-1 00:00:00`
  let end = `${year}-${month}-${lastDay.getDate()} 23:59:59`
  return [start, end]
}

function getFullStrTime (date) {
  let year = date.getFullYear()
  let mouth = fullStr(date.getMonth() + 1 + '')
  let day = fullStr(date.getDate() + '')
  let h = fullStr(date.getHours() + '')
  let m = fullStr(date.getMinutes() + '')
  let s = fullStr(date.getSeconds() + '')
  return `${year}-${mouth}-${day} ${h}:${m}:${s}`
}

function fullStr (str) {
  if (str.length === 1) {
    return '0' + str
  }
  return str
}

export {
  getMerList,
  deleteRow,
  getSkirtList,
  getAllUser,
  getUserGroup,
  getNewsList,
  getTopicsList,
  toExport,
  getPosts,
  getComList,
  deletePrompt,
  getRole,
  getRepoList,
  getLastMouth,
  getFullStrTime
}
